package cn.im.friend;

import cn.im.accid.CheckSumBuilder;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/8.
 */
public class Friendship {
    public static final String appKey = "5ec38e64fd7a39b117e10780d1650377";
    public static final String appSecret = "ceb1e82b9c52";
    public static final String nonce =  "12345";
    public static void buildFriendship(String accid,String faccid,String type) throws UnsupportedEncodingException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/nimserver/friend/add.action";
        HttpPost httpPost = new HttpPost(url);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码

        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", accid));
        nvps.add(new BasicNameValuePair("faccid", faccid));
        nvps.add(new BasicNameValuePair("type", type));
        //返回说明 http 响应:json
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
    }
    //删除好友关系
    public static void deleteFriendship(String accid,String faccid){

    }
    //更新好友关系
    public static void updateFriendship(String accid,String faccid){

    }
    //获得好友关系
    public static void getFriendShip(String accid) throws UnsupportedEncodingException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/nimserver/friend/get.action";
        HttpPost httpPost = new HttpPost(url);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码

        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", accid));
        nvps.add(new BasicNameValuePair("createtime",curTime));
        //返回说明 http 响应:json
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
    }
}
